@echo off
echo ========================================
echo           Vue Frontend Start Script
echo ========================================
echo.

REM Check current directory
echo [INFO] Current working directory: %CD%
echo.

REM Check if nvm is installed
nvm --version >nul 2>&1
if not errorlevel 1 (
    echo [INFO] NVM version manager detected
    nvm --version
    echo [INFO] Available Node.js versions:
    nvm list
    echo.
    
    REM Check current Node.js version
    node --version >nul 2>&1
    if errorlevel 1 (
        echo [ERROR] Node.js not installed or not in PATH
        echo [TIP] Use nvm use version_number to switch to appropriate Node.js version
        echo [TIP] Recommended: Node.js 18.x or 20.x and above
        pause
        exit /b 1
    )
    
    REM Display current version info
    echo [INFO] Currently using Node.js version:
    node --version
    echo [TIP] If version is too low, compatibility issues may occur
    echo [RECOMMEND] Use nvm use 22.15.0 to switch to higher version
) else (
    REM If no nvm, check Node.js directly
    node --version >nul 2>&1
    if errorlevel 1 (
        echo [ERROR] Node.js not installed or not in PATH
        echo [TIP] Please install Node.js or nvm version manager first
        pause
        exit /b 1
    )
)

REM Check and switch to appropriate Node.js version
echo [INFO] Checking Node.js version...

REM Get current Node.js version
for /f "tokens=*" %%i in ('node --version 2^>nul') do set CURRENT_NODE_VERSION=%%i
if "%CURRENT_NODE_VERSION%"=="" (
    echo [ERROR] Node.js is not installed or not available
    echo [TIP] Please install Node.js from https://nodejs.org/
    pause
    exit /b 1
)

echo [INFO] Current Node.js version: %CURRENT_NODE_VERSION%

REM Check if current version is lower than v18
echo %CURRENT_NODE_VERSION% | findstr /r "^v1[0-7]\." >nul
if not errorlevel 1 (
    echo [WARNING] Current Node.js version is too low for this project
    echo [INFO] Attempting to switch to a higher version using nvm...
    
    REM Try to switch to Node.js 22.15.0 first, then 20.x, then 18.x
    nvm use 22.15.0 >nul 2>&1
    if not errorlevel 1 (
        echo [SUCCESS] Switched to Node.js 22.15.0
        goto :version_check_done
    )
    
    REM Try Node.js 20.x versions
    for %%v in (20.18.0 20.17.0 20.16.0 20.15.0) do (
        nvm use %%v >nul 2>&1
        if not errorlevel 1 (
            echo [SUCCESS] Switched to Node.js %%v
            goto :version_check_done
        )
    )
    
    REM Try Node.js 18.x versions
    for %%v in (18.20.0 18.19.0 18.18.0 18.17.0) do (
        nvm use %%v >nul 2>&1
        if not errorlevel 1 (
            echo [SUCCESS] Switched to Node.js %%v
            goto :version_check_done
        )
    )
    
    echo [ERROR] Could not switch to a compatible Node.js version
    echo [TIP] Please install Node.js 18.x or higher using: nvm install 20.18.0
    pause
    exit /b 1
)

:version_check_done
echo [INFO] Final Node.js version:
call node --version
echo [INFO] npm version:
call npm --version
if errorlevel 1 (
    echo [ERROR] npm is not available
    echo [TIP] npm is usually installed with Node.js, please check Node.js installation
    pause
    exit /b 1
)
echo.
echo [INFO] Node.js and npm are ready

REM Check if package.json exists
if not exist "package.json" (
    echo [ERROR] package.json file not found
    echo [TIP] Make sure you are in the correct project directory
    echo [TIP] Current directory: %CD%
    pause
    exit /b 1
)

REM Check if node_modules exists
if not exist "node_modules" (
    echo [WARNING] node_modules directory not found
    echo [INFO] Installing project dependencies...
    echo [TIP] This may take a few minutes, please be patient
    call npm install
    if errorlevel 1 (
        echo [ERROR] Failed to install dependencies
        echo [TIP] Please check network connection and npm configuration
        pause
        exit /b 1
    )
    echo [SUCCESS] Dependencies installed successfully
    echo.
)

echo [INFO] Starting Vue development server...
echo [TIP] You can access the project via browser after startup
echo [TIP] Press Ctrl+C to stop the server
echo [TIP] Usually accessible at: http://localhost:5173
echo ========================================
echo.

REM Start development server
call npm run dev

REM If startup fails, show error message
if errorlevel 1 (
    echo.
    echo ========================================
    echo [ERROR] Failed to start development server
    echo [TIP] Please check the error messages above
    echo [TIP] Common issues and solutions:
    echo   1. Port occupied - Close programs using the port or change port
    echo   2. Dependency version incompatibility - Delete node_modules and reinstall
    echo   3. Node.js version too low - Use nvm to switch to higher version
    echo   4. Network issues - Check firewall and proxy settings
    echo ========================================
    echo [TIP] For technical support, please save the error information above
    pause
    exit /b 1
)

echo.
echo [INFO] Development server stopped
echo [TIP] Thank you for using Vue frontend development environment
pause